using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using QQZiFramework.Membership;

namespace QQZiFramework
{
    /// <summary>
    /// 替换没有实体（主键是redis自增）
    /// </summary>
    public class EmptyEntity : IDBEntity
    {
        [DBField(Usage = DBFieldUsage.PrimaryKey)]
        public virtual string Id { get; set; }
        public Dictionary<string, string> __OVERRIDE_FIELD_NAME__ { get; set; }

        [DBField(Usage = DBFieldUsage.NoField)]
        public string __OVERRIDE_TABLE_NAME__ { get; set; }


        protected static void SetEntity(EmptyEntity newEntity, string tableName, string primaryKey, string primaryValue, Dictionary<string, object> fields)
        {
            newEntity.Id = primaryValue;
            newEntity.__OVERRIDE_FIELD_NAME__ = new Dictionary<string, string>();
            newEntity.__OVERRIDE_FIELD_NAME__.Add("Id", primaryKey);
            newEntity.__OVERRIDE_TABLE_NAME__ = tableName;
            string[] keys = fields.Keys.ToArray();
            int index = 0;
            var properties = newEntity.GetType().GetProperties();
            var maxLen = properties.Length - 3;
            if (maxLen < keys.Length) throw new Exception("超过EmptyEntity定义的属性数量，最多" + maxLen + "个");
            foreach (PropertyInfo property in properties)
            {
                if (!property.Name.StartsWith("Field")) continue;
                if (index == keys.Length) break;
                string keyName = keys[index];
                property.SetValue(newEntity, fields[keyName]);//赋值给Field[n]
                newEntity.__OVERRIDE_FIELD_NAME__.Add(property.Name, keyName);//添加映射
                index++;

            }
        }

        public static EmptyEntity CreateForSave(string tableName, string primaryKey, string primaryValue, Dictionary<string, object> fields)
        {
            var entity = new EmptyEntity();
            SetEntity(entity, tableName, primaryKey, primaryValue, fields);
            return entity;
        }

        public static EmptyIncrEntity CreateIncrForSave(string tableName, string primaryKey, string primaryValue, Dictionary<string, object> fields)
        {
            var entity = new EmptyIncrEntity();
            SetEntity(entity, tableName, primaryKey, primaryValue, fields);
            return entity;
        }

        // public static EmptyEntity CreateForSave(string tableName, string primaryKey, string primaryValue, Dictionary<string, string> fields)
        // {
        //     var entity = new EmptyEntity();
        //     SetEntity(entity, tableName, primaryKey, primaryValue, fields);
        //     return entity;
        // }

        // public static EmptyIncrEntity CreateIncrForSave(string tableName, string primaryKey, string primaryValue, Dictionary<string, string> fields)
        // {
        //     var entity = new EmptyIncrEntity();
        //     SetEntity(entity, tableName, primaryKey, primaryValue, fields);
        //     return entity;
        // }


        public object Field1 { get; set; }

        public object Field2 { get; set; }

        public object Field3 { get; set; }

        public object Field4 { get; set; }

        public object Field5 { get; set; }

        public object Field6 { get; set; }

        public object Field7 { get; set; }

        public object Field8 { get; set; }

        public object Field9 { get; set; }

        public object Field10 { get; set; }

        public object Field11 { get; set; }

        public object Field12 { get; set; }

        public object Field13 { get; set; }

        public object Field14 { get; set; }

        public object Field15 { get; set; }

        public object Field16 { get; set; }

        public object Field17 { get; set; }

        public object Field18 { get; set; }

        public object Field19 { get; set; }

        public object Field20 { get; set; }

        public object Field21 { get; set; }

        public object Field22 { get; set; }
        public object Field23 { get; set; }

        public object Field24 { get; set; }

        public object Field25 { get; set; }

        public object Field26 { get; set; }

        public object Field27 { get; set; }

        public object Field28 { get; set; }

        public object Field29 { get; set; }

        public object Field30 { get; set; }


        public object Field31 { get; set; }

        public object Field32 { get; set; }
        public object Field33 { get; set; }

        public object Field34 { get; set; }

        public object Field35 { get; set; }

        public object Field36 { get; set; }

        public object Field37 { get; set; }

        public object Field38 { get; set; }

        public object Field39 { get; set; }



        public object Field40 { get; set; }


        public object Field41 { get; set; }

        public object Field42 { get; set; }
        public object Field43 { get; set; }

        public object Field44 { get; set; }

        public object Field45 { get; set; }

        public object Field46 { get; set; }

        public object Field47 { get; set; }

        public object Field48 { get; set; }

        public object Field49 { get; set; }

        public object Field50 { get; set; }
        public object Field51 { get; set; }
        public object Field52 { get; set; }
        public object Field53 { get; set; }
        public object Field54 { get; set; }
        public object Field55 { get; set; }
        public object Field56 { get; set; }
        public object Field57 { get; set; }
        public object Field58 { get; set; }
        public object Field59 { get; set; }
        public object Field60 { get; set; }
        public object Field61 { get; set; }
        public object Field62 { get; set; }
        public object Field63 { get; set; }
        public object Field64 { get; set; }
        public object Field65 { get; set; }
        public object Field66 { get; set; }
        public object Field67 { get; set; }
        public object Field68 { get; set; }
        public object Field69 { get; set; }
        public object Field70 { get; set; }
        public object Field71 { get; set; }
        public object Field72 { get; set; }
        public object Field73 { get; set; }
        public object Field74 { get; set; }
        public object Field75 { get; set; }
        public object Field76 { get; set; }
        public object Field77 { get; set; }
        public object Field78 { get; set; }
        public object Field79 { get; set; }
        public object Field80 { get; set; }
        public object Field81 { get; set; }
        public object Field82 { get; set; }
        public object Field83 { get; set; }
        public object Field84 { get; set; }
        public object Field85 { get; set; }
        public object Field86 { get; set; }
        public object Field87 { get; set; }
        public object Field88 { get; set; }
        public object Field89 { get; set; }
        public object Field90 { get; set; }
        public object Field91 { get; set; }
        public object Field92 { get; set; }
        public object Field93 { get; set; }
        public object Field94 { get; set; }
        public object Field95 { get; set; }
        public object Field96 { get; set; }
        public object Field97 { get; set; }
        public object Field98 { get; set; }
        public object Field99 { get; set; }
        public object Field100 { get; set; }
        public object Field101 { get; set; }
        public object Field102 { get; set; }
        public object Field103 { get; set; }
        public object Field104 { get; set; }
        public object Field105 { get; set; }
        public object Field106 { get; set; }
        public object Field107 { get; set; }
        public object Field108 { get; set; }
        public object Field109 { get; set; }
        public object Field110 { get; set; }
        public object Field111 { get; set; }
        public object Field112 { get; set; }
        public object Field113 { get; set; }
        public object Field114 { get; set; }
        public object Field115 { get; set; }
        public object Field116 { get; set; }
        public object Field117 { get; set; }
        public object Field118 { get; set; }
        public object Field119 { get; set; }
        public object Field120 { get; set; }
        public object Field121 { get; set; }
        public object Field122 { get; set; }
        public object Field123 { get; set; }
        public object Field124 { get; set; }
        public object Field125 { get; set; }
        public object Field126 { get; set; }
        public object Field127 { get; set; }
        public object Field128 { get; set; }
        public object Field129 { get; set; }
        public object Field130 { get; set; }
        public object Field131 { get; set; }
        public object Field132 { get; set; }
        public object Field133 { get; set; }
        public object Field134 { get; set; }
        public object Field135 { get; set; }
        public object Field136 { get; set; }
        public object Field137 { get; set; }
        public object Field138 { get; set; }
        public object Field139 { get; set; }
        public object Field140 { get; set; }
        public object Field141 { get; set; }
        public object Field142 { get; set; }
        public object Field143 { get; set; }
        public object Field144 { get; set; }
        public object Field145 { get; set; }
        public object Field146 { get; set; }
        public object Field147 { get; set; }
        public object Field148 { get; set; }
        public object Field149 { get; set; }
        public object Field150 { get; set; }



    }

    /// <summary>
    /// 替换没有实体（主键是表自增）
    /// </summary>
    public class EmptyIncrEntity : EmptyEntity
    {
        [DBField(Usage = DBFieldUsage.PrimaryKey, Identity = true)]
        public override string Id { get; set; }



    }
}